//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "RendistatoBasket.h"
using namespace Cephei::QL::Instruments::Bonds;
#include <gen/QL/Instruments/Bonds/BTP.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (Cephei::Core::IVector<Cephei::QL::Instruments::Bonds::IBTP^>^ btps, Cephei::Core::IVector<Double>^ outstandings, Cephei::Core::IVector<Cephei::QL::IQuote^>^ cleanPriceQuotes) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phRendistatoBasket = NULL;
#endif
        btps ->Lock ();
        INativeVector<Cephei::QL::Instruments::Bonds::IBTP^>^ _NCIbtps = btps->getFeature (NativeFeature::shared_ptr);
        CBTPVector^ _NCbtps = safe_cast<CBTPVector^>(_NCIbtps);
        std::vector<boost::shared_ptr<QuantLib::BTP> >& _btps = static_cast<std::vector<boost::shared_ptr<QuantLib::BTP> >&> (_NCbtps->GetShared ());
        outstandings->Lock();
        INativeVector<Double>^ _NCIoutstandings = outstandings->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCoutstandings = safe_cast<CDoubleVector^>(_NCIoutstandings);
        std::vector<QuantLib::Real>& _outstandings = static_cast<std::vector<QuantLib::Real>&> (_NCoutstandings->GetReference ());
        cleanPriceQuotes ->Lock ();
        INativeVector<Cephei::QL::IQuote^>^ _NCIcleanPriceQuotes = cleanPriceQuotes->getFeature (NativeFeature::Handle);
        CQuoteVector^ _NCcleanPriceQuotes = safe_cast<CQuoteVector^>(_NCIcleanPriceQuotes);
        std::vector<Handle<QuantLib::Quote> >& _cleanPriceQuotes = static_cast<std::vector<Handle<QuantLib::Quote> >&> (_NCcleanPriceQuotes->GetHandle ());
        _ppRendistatoBasket = new boost::shared_ptr<QuantLib::RendistatoBasket> (new QuantLib::RendistatoBasket ( _btps,  _outstandings,  _cleanPriceQuotes ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (btps != nullptr) btps->Unlock();    //not optional
        if (outstandings != nullptr) outstandings->Unlock();    //not optional
        if (cleanPriceQuotes != nullptr) cleanPriceQuotes->Unlock();    //not optional
    }
}
Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (boost::shared_ptr<QuantLib::RendistatoBasket>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoBasket = NULL;
#endif
	_ppRendistatoBasket = &childNative;
    
}
Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (QuantLib::RendistatoBasket& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phRendistatoBasket = NULL;
#endif
	_ppRendistatoBasket = new boost::shared_ptr<QuantLib::RendistatoBasket> (&childNative);
    
    _RendistatoBasketOwner = owner;
    
}

Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (CRendistatoBasket^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoBasket = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRendistatoBasket = new boost::shared_ptr<QuantLib::RendistatoBasket> (copy->GetShared());
        
    }
}
Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoBasket = NULL;
#endif
	if (!t->IsSubclassOf(CRendistatoBasket::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (QuantLib::Handle<QuantLib::RendistatoBasket>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRendistatoBasket = &childNative;
	_ppRendistatoBasket = &static_cast<boost::shared_ptr<QuantLib::RendistatoBasket>>(childNative.currentLink());
    
    _RendistatoBasketOwner = owner;
}
Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (QuantLib::Handle<QuantLib::RendistatoBasket> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRendistatoBasket = &childNative;
	_ppRendistatoBasket = &static_cast<boost::shared_ptr<QuantLib::RendistatoBasket>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::Bonds::CRendistatoBasket::CRendistatoBasket (QuantLib::RendistatoBasket childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoBasket = NULL;
#endif
	_ppRendistatoBasket = new boost::shared_ptr<QuantLib::RendistatoBasket> (new QuantLib::RendistatoBasket (childNative));
    
}
#endif

Cephei::QL::Instruments::Bonds::CRendistatoBasket::~CRendistatoBasket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppRendistatoBasket != NULL)
    {
	    delete _ppRendistatoBasket;
        _ppRendistatoBasket = NULL;
    }
}
Cephei::QL::Instruments::Bonds::CRendistatoBasket::!CRendistatoBasket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppRendistatoBasket != NULL)
    {
	    delete _ppRendistatoBasket;
    }
}
QuantLib::RendistatoBasket& Cephei::QL::Instruments::Bonds::CRendistatoBasket::GetReference ()
{
    if (_ppRendistatoBasket == NULL) throw REFNEW NativeNullException ();
	return **_ppRendistatoBasket;
}
boost::shared_ptr<QuantLib::RendistatoBasket>& Cephei::QL::Instruments::Bonds::CRendistatoBasket::GetShared ()
{
    if (_ppRendistatoBasket == NULL) throw REFNEW NativeNullException ();
	return *_ppRendistatoBasket;
}
QuantLib::RendistatoBasket* Cephei::QL::Instruments::Bonds::CRendistatoBasket::GetPointer ()
{
    if (_ppRendistatoBasket == NULL) throw REFNEW NativeNullException ();
	return &**_ppRendistatoBasket;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RendistatoBasket>& Cephei::QL::Instruments::Bonds::CRendistatoBasket::GetHandle ()
{
	if (_phRendistatoBasket == NULL)
	{
		_phRendistatoBasket = new Handle<QuantLib::RendistatoBasket> (*_ppRendistatoBasket);
	}
	return *_phRendistatoBasket;
}
#endif
bool Cephei::QL::Instruments::Bonds::CRendistatoBasket::HasNative () 
{
	return (_ppRendistatoBasket != NULL);
}

Cephei::Core::IVector<Cephei::QL::Instruments::Bonds::IBTP^>^ Cephei::QL::Instruments::Bonds::CRendistatoBasket::Btps::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::BTP> >& _rv = (std::vector<boost::shared_ptr<QuantLib::BTP> >&)(*_ppRendistatoBasket)->btps ( );   
        CoVector<Cephei::QL::Instruments::Bonds::IBTP^>^ _nrv = REFNEW CoVector<Cephei::QL::Instruments::Bonds::IBTP^>(REFNEW CBTPVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::IQuote^>^ Cephei::QL::Instruments::Bonds::CRendistatoBasket::CleanPriceQuotes::get ()
{
    try
    {
    	std::vector<Handle<QuantLib::Quote> >& _rv = (std::vector<Handle<QuantLib::Quote> >&)(*_ppRendistatoBasket)->cleanPriceQuotes ( );   
        CoVector<Cephei::QL::IQuote^>^ _nrv = REFNEW CoVector<Cephei::QL::IQuote^>(REFNEW CQuoteVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::Bonds::CRendistatoBasket::Outstanding::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRendistatoBasket)->outstanding ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoBasket::Outstandings::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppRendistatoBasket)->outstandings ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Instruments::Bonds::CRendistatoBasket::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppRendistatoBasket)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::Bonds::IRendistatoBasket^ Cephei::QL::Instruments::Bonds::CRendistatoBasket::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppRendistatoBasket)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoBasket::Weights::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppRendistatoBasket)->weights ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::Bonds::IRendistatoBasket^ Cephei::QL::Instruments::Bonds::CRendistatoBasket_Factory::Create (Cephei::Core::IVector<Cephei::QL::Instruments::Bonds::IBTP^>^ btps, Cephei::Core::IVector<Double>^ outstandings, Cephei::Core::IVector<Cephei::QL::IQuote^>^ cleanPriceQuotes)
{
    return REFNEW CRendistatoBasket ( btps,  outstandings,  cleanPriceQuotes);
}
